package com.jh.user.business.impl;



import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.persistence.EntityManager;

import cn.jh.common.exception.ServiceException;
import com.jh.user.pojo.ShopRegister;
import com.jh.user.repository.ShopRegisterRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jh.user.business.UserShopsBusiness;
import com.jh.user.pojo.UserShops;
import com.jh.user.repository.ResourceRepository;
import com.jh.user.repository.UserShopsRepository;



@Service
public class UserShopsBusinessImpl implements UserShopsBusiness{




	@Autowired
	private EntityManager em;
	/*
	 * 商铺管理
	 */
	@Autowired
	private UserShopsRepository  userShopRepository;

	@Autowired
	private ShopRegisterRepository shopRegisterRepository;

	@Transactional
	@Override
	public UserShops addUserShops(UserShops us) {
		// TODO Auto-generated method stub
		UserShops result =  userShopRepository.save(us);
		em.flush();
		return result;
	}

	@Override
	public UserShops findUserShopsById(long id) {
		// TODO Auto-generated method stub
		return userShopRepository.findUserShopsById(id);

	}

	@Override
	public List<UserShops> findUserShops() {
		// TODO Auto-generated method stub

		return userShopRepository.findUserShops();
	}

	@Override
	public UserShops findUserShopsByUid(long userid) {
		// TODO Auto-generated method stub
		return userShopRepository.findUserShopsByUid(userid);
	}

	/**
	 * 修改商户审核状态
	 * **/
	@Override
	public UserShops updateUserShopsStatusByUid(long userid,String status){

		return userShopRepository.updateUserShopsByUid(userid, status) ;
	}

	//获取shops表中随机的userid
	@Override
	public String[] queryRandomUseridByAll() {
		return userShopRepository.queryRandomUseridByAll();
	}

	//获取shops表中随机的shop id
	@Override
	public String[] queryRandomShopIdByAll() {
		return userShopRepository.queryRandomShopIdByAll();
	}

	@Override
	public Map<String, Object> getByShopId(String shopId) throws ServiceException {
		ShopRegister sp = shopRegisterRepository.getByShopId(shopId);
		if (null == sp) {
			throw new ServiceException("无商户信息！");
		}
		String shopId1 = sp.getShopId();
		Integer status = sp.getStatus();
		Long userId = sp.getUserId();
		long brandId = sp.getBrandId();

		Map<String, Object> map = new HashMap<>();
		map.put("shopId", shopId1);
		map.put("status", status);
		map.put("userId", userId);
		map.put("brandId", brandId);
		return map;
	}

}
